
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: AttachedField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import org.dellroad.stuff.string.ParseContext;

/**
 * The {@code "sidekarAttached"} implicit field.
 *
 * <p>
 * This is the implicit field with name {@link Sidekar#ATTACHED_FIELD_NAME} containing whether
 * an item is {@link Item#isSidekarAttached attached}.
 * </p>
 *
 * <p>
 * Unlike normal fields, this field may be accessed even if the associated {@link Item} is detached.
 * </p>
 *
 * @see Item#isSidekarAttached
 * @see Database#getAttachedField
 */
public final class AttachedField extends ImplicitField<Boolean> {

    AttachedField() {
        super(Sidekar.ATTACHED_FIELD_NAME, Boolean.class);
    }

    @Override
    public Boolean get(Item<?> item) {
        return item.isSidekarAttached();
    }

    @Override
    public void visit(FieldSwitch fieldSwitch) {
        fieldSwitch.caseAttachedField(this);
    }

    @Override
    public String toString(Boolean value) {
        return value.toString();
    }

    @Override
    public Boolean fromString(ParseContext ctx) {
        return Boolean.parseBoolean(ctx.matchPrefix("true|false").group());
    }
}

